package pe.gob.pnp.servlets;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletOutputStream;
import org.apache.log4j.Logger;
import pe.gob.pnp.actions.DictamenBean;
import pe.gob.pnp.despachadores.DictamenDespachador;
import pe.gob.pnp.digitalizar.beans.DictamenDetalle;
import pe.gob.pnp.digitalizar.beans.PersonalBean;
import pe.gob.pnp.digitalizar.despachadores.PersonalDespachador;
import pe.gob.pnp.libreria.Utiles;

/**
 *
 * @author ECAMPOS
 */
public class PdfDictamen {

    private static final Logger logger = Logger.getLogger(PdfDictamen.class);
    private Document  document;
    private BaseFont bf;
    private Font arial11;

    public Document crearPdfDictamen(ServletOutputStream out,String ndict_codigo){
        document = new Document(PageSize.A4);
        inicializaFuentes();
         try{

            DictamenBean bdictamen = DictamenDespachador.obtenerDictamenPorId(Integer.parseInt(ndict_codigo));
            PersonalBean policia = PersonalDespachador.obtenerPersonalPolicial( bdictamen.getIdPolicial() );
           

            PdfWriter.getInstance(document, out);
            //Izquierda, Derecha, Superior, Inferior
            document.setMargins(0,0,22,20) ;
            document.setMarginMirroring(true);
            //document.setMargins(0.5f,2.5f,1.5f,1.0f) ;
            document.open();

            crearCabecera(bdictamen, policia);
            crearCuerpo(bdictamen);
            crearPie(bdictamen);

            document.close();

        }catch(Exception e){
            e.printStackTrace();
        }
        return document;
    }

    private void crearCabecera(DictamenBean bdictamen, PersonalBean policia) {
        try {
            String t_area = policia.getNgrad_tipogradoOficina();

            PdfPTable table = new PdfPTable(4);
            PdfPCell cell;

            Chunk chunk = new Chunk("DICTAMEN N· "+ bdictamen.getNdict_identificador() + "-" + Utiles.DateToString( bdictamen.getDdict_fecha(), "yyyy") +"-DIRPEN-PNP/" + t_area ,arial11);
            chunk.setUnderline(0.2f, -2f);
            Paragraph paragraph = new Paragraph("");
            paragraph.add(chunk);

            cell = new PdfPCell(paragraph);
            cell.setColspan(4);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase("Señor: ",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( "General PNP" ,arial11));
            cell.setColspan(3);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase(" ",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( "Director de Pensiones de la PNP " ,arial11));
            cell.setColspan(3);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase("REF.: ",arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);
            cell= new PdfPCell(new Phrase( bdictamen.getCdict_referencia() ,arial11));
            cell.setColspan(3);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( bdictamen.getCdict_solicita() ,arial11));
            cell.setColspan(4);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);

            document.add(table);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void crearCuerpo(DictamenBean bdictamen) {
        try {
            PdfPTable table = new PdfPTable(4);
            PdfPCell cell;

            List<DictamenDetalle> uno =  DictamenDespachador.obtenerDetalleDictamen( bdictamen.getNdict_identificador(), 1);
            List<DictamenDetalle> dos =  DictamenDespachador.obtenerDetalleDictamen( bdictamen.getNdict_identificador(), 2);

            cell= new PdfPCell(new Phrase( "Del estudio de los actuados se despende lo siguiente:" ,arial11));
            cell.setColspan(4);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);

            //Observaciones
            int numero = 0 ;
            for(DictamenDetalle obj : uno){
                cell= new PdfPCell(new Phrase( ++numero + "\t\t " + obj.getCdedict_detalle() ,arial11));
                cell.setColspan(4);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
                cell.setPadding(2f);
                cell.setBorder(0);
                table.addCell(cell);
            }

            cell= new PdfPCell(new Phrase( "Por lo expuesto, esta Asesoría Jurídica OPINA:" ,arial11));
            cell.setColspan(4);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);

            //resuelve
            numero = 0 ;
            for(DictamenDetalle obj : dos){
                cell= new PdfPCell(new Phrase( ++numero + "\t\t " +  obj.getCdedict_detalle() ,arial11));
                cell.setColspan(4);
                cell.setHorizontalAlignment(PdfPCell.ALIGN_JUSTIFIED);
                cell.setPadding(2f);
                cell.setBorder(0);
                table.addCell(cell);
            }
             document.add(table);
        } catch (Exception e) {
            e.printStackTrace();
        }
       
    }

    private void crearPie(DictamenBean bdictamen) {
        try {
            PdfPTable table = new PdfPTable(4);
            PdfPCell cell;

            cell= new PdfPCell(new Phrase( bdictamen.getCdict_instructor() ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setPadding(10f);
            cell.setPaddingTop(30f);
            cell.setBorder(0);
            table.addCell(cell);

            cell= new PdfPCell(new Phrase( Utiles.DateToString(new Date(), "EEEEE dd MMMMM yyyy") ,arial11));
            cell.setColspan(3);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
            cell.setPadding(10f);
            cell.setPaddingTop(30f);
            cell.setBorder(0);
            table.addCell(cell);

            logger.debug( "FOLIOS = " + bdictamen.getCdict_folios());

            cell= new PdfPCell(new Phrase( bdictamen.getCdict_folios() ,arial11));
            cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            cell.setPadding(10f);
            cell.setBorder(0);
            table.addCell(cell);


            document.add(table);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void inicializaFuentes() {
        bf = null;
        try {
            bf = BaseFont.createFont(BaseFont.RESOURCE_PATH +"arial.ttf", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        } catch (Exception ex) {
           logger.error(ex.getMessage());
        }
        arial11 = new Font(bf, 11);
    }

}
